package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;
import com.wl156.widget.ProviceSelector;

public class UserBuchong extends BaseActivity implements OnClickListener {

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	EditText mName;
	EditText mNick;
	RadioGroup mSex;
	RadioButton mNan;
	RadioButton mNv;
	EditText mBirth;
	EditText mCard;
	// EditText mQuyu;
	ProviceSelector mProviceSelector;
	EditText mPhone;
	EditText mCell;
	EditText mQQ;
	EditText mMSN;
	EditText mWebsite;
	EditText mEMail;
	EditText mAddress;
	EditText mMark;

	Button mUpdate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_user_buchong);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mName = (EditText) findViewById(R.id.name);
		mNick = (EditText) findViewById(R.id.nick);
		mSex = (RadioGroup) findViewById(R.id.sex);
		mNan = (RadioButton) findViewById(R.id.nan);
		mNv = (RadioButton) findViewById(R.id.nv);
		mBirth = (EditText) findViewById(R.id.birth);
		mCard = (EditText) findViewById(R.id.card);
		// mQuyu = (EditText) findViewById(R.id.diqu);
		mProviceSelector = (ProviceSelector) findViewById(R.id.provice_selector);
		mProviceSelector.setTitle("区域");
		mPhone = (EditText) findViewById(R.id.phone);
		mCell = (EditText) findViewById(R.id.cell);
		mQQ = (EditText) findViewById(R.id.qq);
		mMSN = (EditText) findViewById(R.id.msn);
		mWebsite = (EditText) findViewById(R.id.website);
		mEMail = (EditText) findViewById(R.id.email);
		mAddress = (EditText) findViewById(R.id.address);
		mMark = (EditText) findViewById(R.id.mark);

		mUpdate = (Button) findViewById(R.id.update);
		mUpdate.setOnClickListener(this);

		new AsyncTask<Void, Void, Object>() {

			ProgressDialog mProgressDialog;

			protected void onPreExecute() {
				mProgressDialog = ProgressDialog.show(UserBuchong.this, "",
						"加载信息中...");
			};

			protected void onPostExecute(Object result) {
				mProgressDialog.dismiss();

				if (result instanceof Exception) {
					Exception e = (Exception) result;
					Toast.makeText(UserBuchong.this, "网络异常，请稍后再试",
							Toast.LENGTH_SHORT).show();
				} else {
					SoapObject so = (SoapObject) result;
					mName.setText(Utils.propertyToShow(so.getProperty(0)));
					mNick.setText(Utils.propertyToShow(so.getProperty(1)));
					String sex = Utils.propertyToShow(so.getProperty(2));
					mSex.check(sex.equals("m") ? R.id.nan : R.id.nv);
					// mSex.
					mBirth.setText(Utils.propertyToShow(so.getProperty(3)));
					mCard.setText(Utils.propertyToShow(so.getProperty(4)));
					// mQuyu.setText(Utils.propertyToShow(so.getProperty(5)));
					mProviceSelector.setProvice(Utils.propertyToShow(so
							.getProperty(5)));
					mProviceSelector.setCity(Utils.propertyToShow(so
							.getProperty(6)));
					mProviceSelector.setRegion(Utils.propertyToShow(so
							.getProperty(7)));
					mPhone.setText(Utils.propertyToShow(so.getProperty(8)));
					mCell.setText(Utils.propertyToShow(so.getProperty(9)));
					mQQ.setText(Utils.propertyToShow(so.getProperty(10)));
					mMSN.setText(Utils.propertyToShow(so.getProperty(11)));
					mWebsite.setText(Utils.propertyToShow(so.getProperty(12)));
					mEMail.setText(Utils.propertyToShow(so.getProperty(13)));
					mAddress.setText(Utils.propertyToShow(so.getProperty(14)));
					mMark.setText(Utils.propertyToShow(so.getProperty(15)));
				}
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.common_GetMyInfo(mCertificate);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();
	}

	@Override
	public void onClick(View v) {

		final String name = mName.getText().toString();
		final String nickName = mNick.getText().toString();
		final String sex = mSex.getCheckedRadioButtonId() == R.id.nan ? "m"
				: "f";
		final String birthday = mBirth.getText().toString();
		final String identification = mCard.getText().toString();
		final String province = mProviceSelector.getProvice();
		final String cities = mProviceSelector.getCity();
		final String districts = mProviceSelector.getRegion();
		final String tel = mPhone.getText().toString();
		final String phone = mCell.getText().toString();
		final String qq = mQQ.getText().toString();
		final String msn = mMSN.getText().toString();
		final String website = mWebsite.getText().toString();
		final String email = mEMail.getText().toString();
		final String address = mAddress.getText().toString();
		final String remarks = mMark.getText().toString();

		new AsyncTask<Void, Void, Object>() {

			protected void onPostExecute(Object result) {
				if (result instanceof Exception) {
					Exception e = (Exception) result;
					Toast.makeText(UserBuchong.this, "网络异常，请稍后再试",
							Toast.LENGTH_SHORT).show();
				} else if (result instanceof SoapPrimitive) {
					SoapPrimitive sp = (SoapPrimitive) result;

					Toast.makeText(UserBuchong.this, sp.toString(),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(UserBuchong.this, "修改成功", Toast.LENGTH_SHORT)
							.show();
				}
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.common_SetMyInfo(mCertificate,
							name, nickName, sex, birthday, identification,
							province, cities, districts, tel, phone, qq, msn,
							website, email, address, remarks);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}

		}.execute();
	}
}
